package com.tfjybj.itoo.exam.provider.controller;

import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.exam.entity.ExamRoomEntity;
import com.tfjybj.itoo.exam.model.ExamRoomModel;
import com.tfjybj.itoo.exam.model.MailInfoModel;
import com.tfjybj.itoo.exam.provider.service.ExamRoomService;
import com.tfjybj.itoo.exam.template.ExamRoomStuTeaModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * ExamRoomController
 * examRoom表
 *
 * @author 任晓帅
 * @version 2.0.0
 * @since 2.0.0 2018-11-15 19:40:28
 */
@Api(tags = {"examRoom表接口"})
@RequestMapping(value = "/examRoom")
@RestController
public class ExamRoomController {

    @Resource
    private ExamRoomService examRoomService;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model ExamRoomModel
     * @return 添加的结果
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody ExamRoomModel model) {
		if (StringUtils.isEmpty(model.getExamId())){
            return ItooResult.build(ItooResult.FAIL, "examId为空");
        }
		if (StringUtils.isEmpty(model.getRoomCapacity())){
            return ItooResult.build(ItooResult.FAIL, "roomCapacity为空");
        }
        ExamRoomEntity examRoomEntity = new ExamRoomEntity();
        BeanUtils.copyProperties(model, examRoomEntity);
        examRoomService.save(examRoomEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功          
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        examRoomService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        examRoomService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model ExamRoomModel
     * @return 修改后的结果
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据id修改examRoom")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody ExamRoomModel model) {
		if (StringUtils.isEmpty(model.getExamId())){
            return ItooResult.build(ItooResult.FAIL, "examId为空");
        }
		if (StringUtils.isEmpty(model.getRoomCapacity())){
            return ItooResult.build(ItooResult.FAIL, "roomCapacity为空");
        }
        ExamRoomEntity examRoomEntity = new ExamRoomEntity();
        BeanUtils.copyProperties(model, examRoomEntity);
        examRoomService.updateById(examRoomEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找ExamRoom
     *
     * @param id 主键id
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        ExamRoomEntity examRoomEntity = examRoomService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", examRoomEntity);
    }

    /**
     * 分页查询所有ExamRoom
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "分页查询所有ExamRoom")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo",value = "页码",required = true,example = "1")@PathVariable Integer pageNo,
								   @ApiParam(name = "pageSize",value = "页数",required = true,example = "10")@PathVariable Integer pageSize) {
        PageInfo<ExamRoomEntity> examRooms = examRoomService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", examRooms);
    }
	
	/**
     * 根据考场名称模糊查询examRoom
     * @param name  考场名称
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的examRoom
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据考场名称模糊查询examRoom", notes = "分页根据考场名称模糊查询examRoom")
    @GetMapping(value = "queryExamRoomByLikeName/{pageNo}/{pageSize}")
    public ItooResult queryExamRoomByLikeName(@RequestParam(required = false, defaultValue = "") String name,
                                              @ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                              @ApiParam(name = "pageSize",value = "页数",required = true,example = "10") @PathVariable Integer pageSize) {
        PageInfo<ExamRoomEntity> examRoomList = examRoomService.queryByLikeName(name, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", examRoomList);
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
    * 查询考场详情
    * @author      王雪芬
    * @return      examRoomModels
    * @param
    * @since 2.0.0        2018-11-19 12:07:46
    */
    @ApiOperation(value = "查询考场详情")
    @PostMapping(value = {"queryExaminRoot/{courseId}/{examNameId}"})
    public ItooResult queryExaminRoot(
            @ApiParam(value = "课程id", required = true) @PathVariable String courseId,
            @ApiParam(value = "考试id", required = true) @PathVariable String examNameId
//
//            @RequestParam(required = false) String courseId,
//            @RequestParam(required = false) String examNameId
    ){
//        if(courseId !=null || courseId != "" || examNameId !=null || examNameId != "" ) {
            List<ExamRoomModel> examRoomModels = examRoomService.queryExaminRoot(courseId, examNameId);
            return ItooResult.build(ItooResult.SUCCESS,"查询成功",examRoomModels);
//        } else {
//            return ItooResult.build(ItooResult.FAIL,"没有选择课程！");
//        }
    }
    /**
     * 发送考试情况通报表
     * @author      王雪芬
     * @return             大于0则是成功,小于1
     * @since 2.0.0        2018-11-19 12:07:46
     */
    @ApiOperation(value = "发送考试情况通报表")
    @PostMapping(value = {"sendExamWorkInfo"})
    public  ItooResult sendExamWorkInfo(@RequestBody MailInfoModel info){
        if(ObjectUtils.isEmpty(info)){
            return ItooResult.build(ItooResult.FAIL,"examinationModel不能为空");
        }
      int flag= examRoomService.sendExamWorkInfo( info);
     if(flag>0){
         return ItooResult.build(ItooResult.SUCCESS,"发送成功");
     }else {
         return ItooResult.build(ItooResult.FAIL,"发送失败");
     }

    }

    /**
     * 完成选择时插入考场和人数以及监考老师(单个有序插入)
     *
     * @param jsonList
     * @return 受影响行数
     * @author maying
     * @since 2018-12-14 08:48:08
     */
    @ApiOperation(value = "插入考生，考场和监考老师")
    @PostMapping(value = "/saveStuExamRoom")
    public ItooResult saveStuExamRoom(@RequestBody ExamRoomStuTeaModel jsonList) {
        if (jsonList == null) {
            return ItooResult.build(ItooResult.FAIL, "数组不能为空！");
        }
        boolean flag = examRoomService.saveStuExamRoom(jsonList);
        if (flag) {
            return ItooResult.build(ItooResult.SUCCESS, "保存成功！", flag);
        }else {
            return ItooResult.build(ItooResult.FAIL,"保存失败!");
        }
    }

    /**
     * 点击考场选择按钮,判断此考场是否选择过
     * @param roomId 教室id
     * @param schoolCode 校区代号
     * @param startTime 考试开始时间
     * @since 2018-12-21 12:00:17
     * @author 马莹
     * @return 查询考场集合
     */
    @ApiOperation(value = "点击考场单选框筛选考场,判断考场是否选择过")
    @GetMapping(value = "/selectExamRoom/{roomId}/{schoolCode}/{startTime}")
    public ItooResult selectExamRoom(@PathVariable String roomId, @PathVariable String schoolCode, @PathVariable String startTime){
        List<ExamRoomModel> examRoomModelList=examRoomService.selectExamRoom(roomId,schoolCode,startTime);
        return ItooResult.build(ItooResult.SUCCESS,"查询成功!",examRoomModelList);
    }

    /**
     * 根据考试id查询所有监考老师,对应考场
     *
     * @param teacherName 模糊参数
     * @param examId 考试id
     * @return
     * @author 马莹
     * @since 2018-12-23 09:43:39
     */
    @ApiOperation(value = "根据考试id查询所有监考老师,对应考场")
    @GetMapping(value = "/findTeacher/{examId}")
    public ItooResult findTeacher(@PathVariable String examId, @RequestParam(required = false,defaultValue = "") String teacherName) {
        List<ExamRoomModel> examRoomModelList = examRoomService.findTeacher(examId,teacherName);
        if (examRoomModelList.size() > 0){
            return ItooResult.build(ItooResult.SUCCESS,"查询成功!",examRoomModelList);
        }else {
            return ItooResult.build(ItooResult.FAIL,"查询失败!");
        }

    }

    /**
     * 根据考试id筛选考场使用情况
     *
     * @param examId 考试id
     * @return List<ExamRoomModel>
     * @author 马莹
     * @since 2018-12-26 15:18:37
     */
    @ApiOperation(value = "根据考试id筛选考场使用情况")
    @GetMapping(value = "/selectExamRoomTodo/{examId}")
    public ItooResult selectExamRoomTodo(@PathVariable String examId) {
        List<ExamRoomModel> examRoomModelList = examRoomService.selectExamRoomIng(examId);
        if (examRoomModelList != null) {
            return ItooResult.build(ItooResult.SUCCESS, "查询成功!", examRoomModelList);
        } else {
            return ItooResult.build(ItooResult.FAIL, "查询失败!");
        }
    }
}
